// pages/leave/leave.js
const db=wx.cloud.database()
const _ = db.command
Page({

    /**
     * 页面的初始数据
     */
    data: {
        show:false,//控制下拉列表的显示隐藏，false隐藏、true显示
        selectData:['4小时','8小时','12小时','24小时'],//下拉列表的数据
        index:0,//选择的下拉列表下标,
        userInfo:'',
        hourMinuteSecond: '',//时分，根据需要选择
        hourMinuteSecond2: '',//时分，根据需要选择
        timeDivision: '',//时分秒，根据需要选择
        courselist:'',
        start_week:0,
        end_week:0,
        start_xqj:0,
        end_xqj:0,
        leavereason:''，
    },
    leavereason: function(e) {
        this.setData({
          leavereason:e.detail.value
        }) 
    }, 
    login(){
        let id =wx.getStorageSync('userInfo').id
        let openid=wx.getStorageSync('userInfo')._openid
        let name=wx.getStorageSync('userInfo').name
        
        db.collection('course').where({
            ZC:_.gte(this.data.start_week).and(_.lte(this.data.end_week)),
            XQJ:_.gte(this.data.start_xqj).and(_.lte(this.data.end_xqj)),
            XH:Number(id)
        }).get().then(res=>{
            console.log(res)
            db.collection('leave').add({
                data:{
                    _openid:openid,
                    class1:res.data,
                    endtime:this.data.hourMinuteSecond2,
                    id:id,
                    name:name,
                    remark:this.data.leavereason,
                    progress:'0',
                    result:'0',
                    starttime:this.data.hourMinuteSecond,
                    update:'0'
                }
            }).then(res=>{
                wx.switchTab({
                    url: '/pages/function/function',
                  })
            })
        })

    },
    // 点击下拉显示框
    selectTap(){
        this.setData({
        show: !this.data.show
        });
    },
    // 点击下拉列表
    optionTap(e){
     let Index=e.currentTarget.dataset.index;//获取点击的下拉列表的下标
     this.setData({
        index:Index,
        show:!this.data.show
     });
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        this.setData({
            userInfo:wx.getStorageSync('userInfo')
        })

    },
    getWeekDate(date) {
        var now = new Date(date);
        var day = now.getDay();
        var weeks = new Array(
          7,
          1,
          2,
          3,
          4,
          5,
          6
        );
        var week = weeks[day];
        return week;
      },

      // 获取当前周
getNowWeek(str1){
    var date = new Date(str1);
        let [year, month, day] = ['2022','02','21']
        var start = new Date(year, month-1, day);
        //计算时间差
        var left_time = parseInt((date.getTime()-start.getTime())/1000)+24 * 60 * 60;
        var days = parseInt(left_time/3600/24);
        var week = Math.floor(days / 7) + 1;
        var result = week
        if(week>20 || week <= 0){
          result = this.data.now_week;
        }
        return result
},
    selectDateMinuteChange(e) {
        this.setData({
          hourMinuteSecond: e.detail.value
        })
        var str=this.data.hourMinuteSecond.split(' ')
        var yymmdd=str[0].split('-')
        var hhmm=str[1].split(':')
        this.setData({
            start_week:this.getNowWeek(str[0]),
            start_xqj:this.getWeekDate(str[0])
        })
      },
      selectDateMinuteChange2(e) {
        this.setData({
          hourMinuteSecond2: e.detail.value
        })
        var str=this.data.hourMinuteSecond2.split(' ')
        var yymmdd=str[0].split('-')
        var hhmm=str[1].split(':')
        this.setData({
            end_week:this.getNowWeek(str[0]),
            end_xqj:this.getWeekDate(str[0])
        })
      },
      // 时分秒的事件方法
      selectDateSecondChange(e) {
        this.setData({
          timeDivision: e.detail.value
        })
      },


    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    }
})